﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Caching;
using System.Collections;

namespace SusuCMS.Caching
{
    public static class CmsDataCache
    {
        #region Online Hosts

        public static Dictionary<string, int> OnlineHosts
        {
            get
            {
                if (_OnlineHosts == null)
                {
                    _OnlineHosts = GetOnlineHosts();
                }

                return _OnlineHosts;
            }
        }
        private static Dictionary<string, int> _OnlineHosts;

        private static Dictionary<string, int> GetOnlineHosts()
        {
            var unitOfWork = CmsApplication.GetDataContext();

            var websites = CmsApplication.GetWebsiteRepository(unitOfWork).FindAll()
                .Where(i => i.IsDeleted == false && i.IsOnline == true);

            var data = new Dictionary<string, int>();
            foreach (var website in websites)
            {
                var list = website.GetHosts();
                foreach (var item in list)
                {
                    data.Add(item.ToLower(), website.Id);
                }
            }

            return data;
        }

        public static void UpdateOnlineHosts()
        {
            _OnlineHosts = null;
        }

        #endregion

        #region Online Websites

        public static Dictionary<int, Website> OnlineWebsites
        {
            get
            {
                if (_OnlineWebsites == null)
                {
                    _OnlineWebsites = GetOnlineWebsites();
                }

                return _OnlineWebsites;
            }
        }
        private static Dictionary<int, Website> _OnlineWebsites;

        private static Dictionary<int, Website> GetOnlineWebsites()
        {
            var unitOfWork = CmsApplication.GetDataContext();
            var list = CmsApplication.GetWebsiteRepository(unitOfWork).FindAll()
                .Where(i => i.IsDeleted == false && i.IsOnline == true);

            var data = new Dictionary<int, Website>();
            foreach (var item in list)
            {
                data.Add(item.Id, item);
            }

            return data;
        }

        public static void UpdateOnlineWebsites()
        {
            _OnlineWebsites = null;
        }

        #endregion

        #region Home Pages

        public static Dictionary<int, Page> OnlineHomePages
        {
            get
            {
                if (_OnlineHomePages == null)
                {
                    _OnlineHomePages = GetOnlineHomePages();
                }

                return _OnlineHomePages;
            }
        }
        private static Dictionary<int, Page> _OnlineHomePages;

        private static Dictionary<int, Page> GetOnlineHomePages()
        {
            var unitOfWork = CmsApplication.GetDataContext();
            var list = CmsApplication.GetPageRepository(unitOfWork).FindAll()
                .Where(i => i.IsPublished == true && i.Url.Equals("index", StringComparison.OrdinalIgnoreCase));

            var data = new Dictionary<int, Page>();
            foreach (var item in list)
            {
                data.Add(item.WebsiteId, item);
            }

            return data;
        }

        public static void UpdateOnlineHomePages()
        {
            _OnlineHomePages = null;
        }

        #endregion
    }
}
